<?php
namespace App\Http\Controllers\School;

use App\Models\SchoolAdmin;
use App\Models\SchoolAdminLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

/**
 * Class TokenController
 * @package App\Http\Controllers\School
 * @author zjw
 * token
 */
class TokenController extends BaseController {

    public function store(Request $request){
        $phone = $request->username;
        $password = $request->password;
        $admin = SchoolAdmin::where([
            'phone'=>$phone,
        ])->first();
        if ($admin && Hash::check($password,$admin->password)) {
            if($admin->status == 0) {
                return $this->fail('该账号已停止使用');
            }
            if(!$admin->school || $admin->school->status == 0){
                return $this->fail('该账号所属学校已停止使用');
            }
            $token = str_random(40);
            $admin->access_token = $token;
            $admin->expire_time = date("Y-m-d H:i:s",time()+ 30 * 60);
            $admin->last_login_at = date("Y-m-d H:i:s");
            if($admin->save()){
                SchoolAdminLog::Log('登录',$admin);
                return $this->success($token,'token');
            }
            return $this->fail('发生未知错误');
        }
        return $this->fail('账号密码错误');
    }
}

